<?php
/**
 * 文章分类
 * ============================================================================
 * 版权所有 2005-2010 KnifeCMS 并保留所有权利。
 * 网站地址: http://www.knifecms.com；
 * ----------------------------------------------------------------------------
*/
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
$smarty->caching = false;
/* 清除缓存 */
//clear_cache_files();
/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */
/* 获得指定的分类ID */
if (!empty($_GET['id']))
{
    $cat_id = intval($_GET['id']);
}
elseif (!empty($_GET['category']))
{
    $cat_id = intval($_GET['category']);
}
else
{
    ecs_header("Location: ./\n");
    exit;
}
$article_id     = 0;
if($cat_id)
{
    $article_id = $db->getOne("SELECT article_id FROM " . $ecs->table('article') . " WHERE cat_id = '".intval($cat_id)."'  ORDER BY article_type DESC,length(seq) DESC, seq DESC, article_id DESC ");
}
$cat_url = get_article_cat_url($cat_id);
if($cat_url){
	if(str_len($cat_url)>0){
		ecs_header("Location: $cat_url\n");
		exit;
	}
}
$cat_name = get_article_cat_name($cat_id);
$root_id = get_article_root($cat_id);
$root_name = get_article_cat_name($root_id);
$show_type = get_article_cat_type($cat_id);
if($show_type==1)
{
    $Loaction = get_article_show_location($cat_id);
    if (!empty($Loaction))
    {
        ecs_header("Location: $Loaction\n");
        exit;
    }
}
$cat = get_article_cat_info($cat_id);
$article = get_article_info($article_id);
/* 获得当前页码 */
$page = !empty($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */
/* 获得页面的缓存ID */
//$cache_id = sprintf('%X', crc32($cat_id . '-' . $page . '-' . $_CFG['lang']));
$template = get_article_cat_template($cat_id);
/*
if (!$smarty->is_cached($template, $cache_id))
{
*/
    /* 如果页面没有被缓存则重新获得页面的内容 */
	assign_template('a', array($cat_id));
	$position = assign_ur_here($cat_id);
	$smarty->assign('page_title',           $position['title']);     // 页面标题
	$smarty->assign('ur_here',              $position['ur_here']);   // 当前位置
	$smarty->assign('cat_name',             $cat_name);   // 分类名称
	$smarty->assign('root_name',            $root_name); 	//根分类名称
	$smarty->assign('categories',           get_categories_tree(0)); // 分类树
	$smarty->assign('sub_cats',				get_sub_cat($cat_id)); //文章分类树
	$smarty->assign('article_categories',   get_article_root_tree($cat_id));
	$smarty->assign('helps',                get_shop_help());        // 网店帮助
	$smarty->assign('top_goods',            get_top10());            // 销售排行
	$smarty->assign('best_goods',           get_recommend_goods('best'));
	$smarty->assign('new_goods',            get_recommend_goods('new'));
	$smarty->assign('hot_goods',            get_recommend_goods('hot'));
	$smarty->assign('promotion_goods',      get_promote_goods());
	$smarty->assign('promotion_info', 		get_promotion_info());
    /* Meta */
    $meta = $db->getRow("SELECT keywords, cat_desc, page_size FROM " . $ecs->table('article_cat') . " WHERE cat_id = '$cat_id'");
    if ($meta === false || empty($meta))
    {
        /* 如果没有找到任何记录则返回首页 */
        ecs_header("Location: ./\n");
        exit;
    }
	$smarty->assign('thisCat',      $cat);
	$smarty->assign('article',      $article);
    $smarty->assign('keywords',    htmlspecialchars($meta['keywords']));
    $smarty->assign('description', htmlspecialchars($meta['cat_desc']));
    /* 获得文章总数 */
    $size   = $meta['page_size'];
    if($size<=0){
    	$size   = isset($_CFG['article_page_size']) && intval($_CFG['article_page_size']) > 0 ? intval($_CFG['article_page_size']) : 10;
    }
    $count  = get_article_count($cat_id);
    $pages  = ($count > 0) ? ceil($count / $size) : 1;
    if ($page > $pages)
    {
        $page = $pages;
    }
    $pager['search']['id'] = $cat_id;
    $keywords = '';
    $goon_keywords = ''; //继续传递的搜索关键词
    /* 获得文章列表 */
    if (isset($_POST['keywords']))
    {
        $keywords = addslashes(urldecode(trim($_POST['keywords'])));
        $pager['search']['keywords'] = $keywords;
        $search_url = substr(strrchr($_POST['cur_url'], '/'), 1);
        $smarty->assign('search_value',     $keywords);
        $smarty->assign('search_url',       $search_url);
        $count  = get_article_count($cat_id, $keywords);
        $pages  = ($count > 0) ? ceil($count / $size) : 1;
        if ($page > $pages)
        {
            $page = $pages;
        }
        $goon_keywords = urlencode($_POST['keywords']);
    }
    $smarty->assign('articles_list',    get_cat_articles($cat_id, $page, $size ,$keywords));
    $smarty->assign('cat_id',    $cat_id);
    /* 分页 */
    assign_pager('article_cat', $cat_id, $count, $size, '', '', $page, $goon_keywords);
    assign_dynamic('article_cat');
//}
$smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-typearticle_cat" . $cat_id . ".xml" : 'feed.php?type=article_cat' . $cat_id); // RSS URL
//$smarty->display($template, $cache_id);
$smarty->display($template, '');
/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得指定的频道的详细信息
 *
 * @access  private
 * @param   integer     $cat_id
 * @return  array
 */
function get_article_cat_info($cat_id){
    $sql="SELECT * FROM " . $GLOBALS['ecs']->table('article_cat') . " WHERE cat_id=" . $cat_id;
    $row = $GLOBALS['db']->getRow($sql);
    
    return $row;
}
/**
 * 获得指定的文章的详细信息
 *
 * @access  private
 * @param   integer     $article_id
 * @return  array
 */
function get_article_info($article_id)
{
    /* 获得文章的信息 */
    $sql = "SELECT a.*, IFNULL(AVG(r.comment_rank), 0) AS comment_rank ".
            "FROM " .$GLOBALS['ecs']->table('article'). " AS a ".
            "LEFT JOIN " .$GLOBALS['ecs']->table('comment'). " AS r ON r.id_value = a.article_id AND comment_type = 1 ".
            "WHERE a.is_open = 1 AND a.article_id = '$article_id' GROUP BY a.article_id";
    $row = $GLOBALS['db']->getRow($sql);
    if ($row !== false)
    {
        $row['comment_rank'] = ceil($row['comment_rank']);                              // 用户评论级别取整
        $row['add_time']     = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); // 修正添加时间显示
        /* 作者信息如果为空，则用网站名称替换 */
        if (empty($row['author']) || $row['author'] == '_SHOPHELP')
        {
            $row['author'] = $GLOBALS['_CFG']['shop_name'];
        }
	    $vote = get_vote($row['vote_id']);
	    if (!empty($vote))
	    {
	        $row['vote'] = $vote['content'];
	    }
    }
    return $row;
}
?>